

package ru.samgtu.wst.util.swing;

import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;


public class TreeUtil {

	/** Creates a new instance of TreeUtil */
	private TreeUtil() {
	}

	public static void expandAll(JTree tree, boolean expand) {
		TreeModel model = tree.getModel();

		// Traverse tree from root
		expandAll(tree, new TreePath(tree.getModel().getRoot()), expand);
	}

	private static void expandAll(JTree tree, TreePath path, boolean expand) {
		Object parent = path.getLastPathComponent();
		int childCount = tree.getModel().getChildCount(parent);
		for (int i = 0; i < childCount; i++) {
			Object child = tree.getModel().getChild(parent, i);
			TreePath childPath = path.pathByAddingChild(child);
			expandAll(tree, childPath, expand);
		}

		// Expansion or collapse must be done bottom-up
		if (expand) {
			tree.expandPath(path);
		} else {
			tree.collapsePath(path);
		}
	}

}
